﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Medianamik.Core.CodeGeneration
{
    [DebuggerDisplay("{Value}")]
    public class SqlType
    {
        private string _value;
        public string Value
        {
            get { return _value; }
            private set { _value = value; }
        }
        private SqlType(string pValue)
        {
            Value = pValue;
        }

        public static SqlType UniqueIdentifier()
        {
            return new SqlType("UNIQUEIDENTIFIER"); 
        }

        public static SqlType Bit()
        {
            return new SqlType("BIT");
        }

        public static SqlType Int()
        {
            return new SqlType("INT");
        }

        public static SqlType Nvarchar(int pLength)
        {
            return new SqlType(string.Concat("NVARCHAR(", pLength, ")"));
        }

        public static SqlType NvarcharMax()
        {
            return new SqlType("NVARCHAR(MAX)");
        }

        public static SqlType SmallInt()
        {
            return new SqlType("SMALLINT");
        }

        public static SqlType DateTime()
        {
            return new SqlType("DATETIME");
        }

        public static SqlType Xml()
        {
            return new SqlType("XML");
        }

        public static SqlType FromNodeTypeProperty(IProperty pProperty)
        {
            var value = pProperty.DataType.SQLDataTypeName;
            if (pProperty.DataType.MustSpecifyLength )
            {
                string maxLength = pProperty.MaxLength.HasValue
                    ? Math.Max((short)1, pProperty.MaxLength.Value).ToString()
                    : "MAX";
                value += string.Concat("(", maxLength, ")");
            }

            return new SqlType(value);
        }

        public override string ToString()
        {
            return Value;
        }
    }
}
